package com.renhui;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.renhui.aidl.IUserVerifyInterface;
import com.renhui.aidl.User;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private EditText mEditName;
    private EditText mEditPassword;

    IUserVerifyInterface userVerifyInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditName = findViewById(R.id.edit_name);
        mEditPassword = findViewById(R.id.edit_pwd);
        findViewById(R.id.btn_commit).setOnClickListener(this);

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.renhui.service","com.renhui.service.UserVerifyService"));
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_commit) {

            String name = mEditName.getText().toString();
            String password = mEditPassword.getText().toString();

            if (userVerifyInterface != null) {
                try {
                    boolean verify = userVerifyInterface.verifyUser(new User(name, password));
                    if (verify) {
                        Toast.makeText(this, "验证成功!!!", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(this, "验证失败!!!", Toast.LENGTH_LONG).show();
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(this, "未绑定服务!!!", Toast.LENGTH_LONG).show();
            }
        }
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.w(TAG, "onServiceConnected: success ");
            userVerifyInterface = IUserVerifyInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.w(TAG, "onServiceDisConnected! ");
            unbindService(serviceConnection);
        }
    };


}
